package com.twq.basic.bigpicture

/**
 * Created by tangweiqun on 2017/11/27.
 * 使用Lists
 */
object Step8 {
  def main(args: Array[String]): Unit = {
    //函数式编程的最主要的思想是：方法应该没有side effect。
    //一个方法应该被计算然后始终返回一个值，这样可以使的方法更可靠和复用
    //所以就使得函数式编程中的对象应该始终是不变的
    //数组是可变的对象
    //1、如果想使用不可变的序列，我们可以用scala.List
    val list = List(1, 2, 3)

    //2、对List进行转换的时候，都会生成一个新的List
    val oneTwo = List(1, 2)
    val threeFour = List(3, 4)
    val oneTwoThreeFour = oneTwo ::: threeFour //:::为对list做拼接
    println(oneTwo + " and " + threeFour + " were not mutated.")
    println("Thus, " + oneTwoThreeFour + " is a new list.")

    //3、创建List
    val oneTwoThree = 1 :: 2 :: 3 :: Nil //Nil代表空的list
    println(oneTwoThree)

    //4、List中含有很多的方法
    val filtered = oneTwoThree.filter(_ != 2)
    oneTwoThree.foreach(println)

    oneTwoThree.map(_ + 1)
    oneTwoThree.flatMap(_ to 5)
    oneTwoThree.reduceLeft(_ + _)
    println(filtered)
  }
}
